Skip to content

chore(agents): stop agents pushing code verify hasn't passed - #135

Merged
scottlovegrove merged 7 commits into
mainfrom
scottl/agent-push-check
Nov 27, 2026
Merged

scottlovegrove merged 7 commits into
mainfrom
scottl/agent-push-check

Conversation

@scottlovegrove

@scottlovegrove scottlovegrove commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Replaces the end-of-turn gate with a push check, ported from todoist/todoist-cli#672. Before each shell command, the Claude Code and Codex hooks (now one PreToolUse hook on Bash, replacing UserPromptSubmit and Stop) call .agents/scripts/guard.ts. It stops an agent's git push or gh pr create from this checkout until npm run verify has passed on exactly the commit being pushed, not the working tree. That's where a person starts reviewing, so that's where the proof has to exist. Pushes a person runs in their own terminal never go through it.

The guard reads the shell command without running it: cd, pushd, git -C, env -C and the other wrappers, subshells, eval, sh -c and env -S strings, refspecs and tags, gh -R and --head, quotes and here-documents (so a commit message that says git push is ignored). It fingerprints the pushed commit from git ls-tree, so the working-tree snapshot now hashes files the way git hashes a blob. What needs no verify follows verify itself: changes it records nothing for (docs, CI-only files), a branch whose code is back to main's, dry runs, deletions and tag-only pushes. A push whose contents can't be told from the command is stopped. For a commit and a push in one command, it checks the working tree only when the commit takes all of it (everything staged first, git commit -a with nothing untracked, or a plain commit with nothing unstaged), and otherwise stops the push. DISABLE_AGENT_GUARD=1 still turns it off, a checkout without the guard is skipped silently, a guard that fails allows the action and says why, and the retired turn-start/turn-end events that older hook configs still send are ignored quietly.

Before this change the tooling in .agents/scripts was the same as todoist-cli's apart from verify's steps and a few comms-specific test rows, so the code started as todoist-cli's as merged, with every fix from its review. The docs keep comms-cli's wording around it. Doistbot's review here found four problems todoist-cli has too, each fixed in its own commit: a branch reset by git switch -C, git checkout -B or git branch -f was checked by its old tip, an inline git -c push.* setting made a push with explicit refspecs read as every branch, a file the snapshot couldn't read counted as deleted, and a commit and push in one command was checked against the whole working tree even when the commit takes less.

Before those fixes, I tried it with real agents at the top of this stack (this PR, #136 and #137), in a throwaway clone pushing to a local bare repository. Claude Code (claude -p) and Codex 0.172 (codex exec, in a plain clone once trusted) were each blocked on an unverified commit with the guard's reason, ran npm run verify, and then pushed; the bare repository's refs matched the pushed commits, and the transcripts showed the hook's denial on the first push.

Codex users need to run codex --trust in their main checkout and review the new hook in /hooks once. In a linked worktree, Codex runs the main checkout's .codex/hooks.json, so a worktree is guarded once the main checkout has this hook.

@scottlovegrove scottlovegrove self-assigned this Nov 27, 2026
@scottlovegrove scottlovegrove changed the title scottl/agent push check chore(agents): stop agents pushing code verify hasn't passed Nov 27, 2026

@doistbot doistbot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR replaces the end-of-turn gate with a PreToolUse push check: a shell/push command parser, a guard that verifies the pushed commit against the verify record, hook wiring, and docs/test coverage.

Few things worth tightening:

  • The cheap pre-filter in both hook configs misses a gh pr create split across lines with a trailing backslash ([[:space:]\\] doesn't match the escaped n), so that payload exits 0 and an unverified PR opens untouched — widen the character class and pin the continuation form with a wiring test.
  • Force-created destinations can slip through: -C / -B (git switch -C copy && git push origin copy) are checked against the old destination tip, which passes the unchanged-from-main exemption even though the push lands the unverified HEAD — resolve names in checkout.created to head first and add a regression test for an existing destination branch.
  • Explicit refspecs are discarded when the command carries inline push.*/remote.*/branch.* config (git -c push.default=matching push origin main reports sources: ['*'] and is wrongly denied), so only extraArgs should replace refspecs — and the guard tests should stub GIT_CONFIG_GLOBAL/GIT_CONFIG_NOSYSTEM like push-check.test.ts does, since bare git push behavior depends on the developer's global config.
  • Guard robustness: suppress only disappearance errors (ENOENT) in the snapshot read catch and rethrow permission/I/O failures so dirty-tree denials aren't misleading; and don't take the working-tree shortcut for git commit -am … && git push, since a partially staged commit holds less than the tree verify hashed — reserve the shortcut for commands that stage everything.

I also included a few optional follow-up notes in the details below.

Optional follow-up notes (9)
  • P3 .agents/scripts/lib/push-targets.ts:91: A push reached through a leading word the reader doesn't recognize is invisible. readCommand only inspects words[start], so nice git push, stdbuf -oL git push, or a command name from a variable — cmd="git push -u origin feature"; $cmd, or $(echo git) push — produce no target and the command is allowed, while $BRANCH in refspec position is deliberately treated as unknown. Consider treating an unrecognized first word that starts with $ as unknown, and adding the common zero-setup wrappers to WRAPPERS.
  • P3 .agents/scripts/guard.test.ts:86: repo.dir.split('/').pop() hand-rolls what basename(repo.dir) already does (it's also the separator-safe API). push-check.test.ts:25 uses the same expression.
  • P3 .agents/scripts/lib/push-check.test.ts:24: This linked worktree is removed only at the end of the test body, so a failing assertion leaves it behind in tmpdir() — TempRepo.cleanup() only removes dir and addOrigin's folders. guard.test.ts builds the same worktree inline. Put addWorktree in test-repo.ts next to addOrigin and track the path in extraDirs, so both tests get cleanup for free and the setup lives in one place.
  • P3 .agents/scripts/guard.test.ts:30: Pass-through wrapper for repo.commitOnBranch that drops the optional content argument and adds nothing. Call repo.commitOnBranch(branch, path) directly in the tests and delete the alias.
  • P3 .agents/scripts/hook-wiring.test.ts:68: The dir option on the run helper is never passed by any call site — every test runs in repo.dir, and the bash payload also hardcodes cwd to repo.dir/src. Drop the option and use repo.dir directly, or wire it up if a worktree case is coming.
  • P3 .agents/scripts/guard.ts:54: The command is parsed twice whenever it contains a push or gh pr create: findTargets runs here to filter, then checkCommand (push-check.ts line 65) calls findTargets(command, cwd) again on the same string. Since the filter exists precisely to keep non-push commands cheap, the push path should pay the parse once too — e.g. pass the parsed targets into checkCommand and let it skip its own re-parse.
  • P3 .agents/scripts/lib/push-check.ts:81: Checkout resolution isn't cached per path, so a command with several pushes or PRs from the same checkout repeats git subprocesses per target: checkoutOf(target.dir) (a rev-parse) and guarded(path) → commonDir(path) (a rev-parse) here, plus configuredPush(path, ...) (several git config calls) at line 104 for each implicit target — e.g. git push && git push runs configuredPush twice against the same checkout. The checkouts map already keys on path; memoize checkoutOf/commonDir results per path (and configuredPush per root) alongside it, the same way openCheckout memoizes defaultRef and tree.
  • P3 .agents/scripts/lib/push-check.test.ts:348: addWorktree creates the worktree beside repo.dir, but repo.cleanup() in afterEach only removes repo.dir (and origins). If any assertion above fails, this worktree remove is skipped and the sibling temp directory leaks. Wrap the test body in try/finally, as guard.test.ts's worktree test already does.
  • P3 .agents/scripts/lib/push-targets.test.ts:35: The expected value for cd ~/code is built the same way the code builds it: join(homedir(), 'code') mirrors resolveDir's join(homedir(), to.slice(2)), so the assertion can't tell expansion from a bug in how ~ is joined. Stub HOME in the test (vi.stubEnv('HOME', '/home/tester')) and expect the literal /home/tester/code.

Share Feedback • Review Logs • Trace

Comment thread .claude/settings.json Resolved
Comment thread .agents/scripts/lib/push-check.ts Resolved
Comment thread .agents/scripts/lib/push-targets.ts Resolved
Comment thread .agents/scripts/lib/tree.ts Resolved
Comment thread .agents/scripts/guard.test.ts Resolved
Comment thread .agents/scripts/lib/push-check.ts Resolved
@scottlovegrove scottlovegrove added the 🚢 Ship PR requires passing checks before merge, but no independent human review. label Nov 27, 2026
Replaces the end-of-turn gate with a push check, ported from
todoist/todoist-cli#672. Before each shell command, the Claude Code and
Codex hooks (now one PreToolUse hook on Bash, replacing UserPromptSubmit
and Stop) call .agents/scripts/guard.ts. It stops an agent's git push or
gh pr create from this checkout until npm run verify has passed on
exactly the commit being pushed, not the working tree. Pushes a person
runs in their own terminal never go through it.

The guard reads the shell command without running it (cd, git -C,
wrappers, eval, sh -c, quotes and here-documents), fingerprints the
pushed commit from git ls-tree, and lets through changes verify records
nothing for, dry runs, deletions and tag-only pushes. A push whose
contents it can't tell is stopped. DISABLE_AGENT_GUARD=1 still turns it
off, a checkout without the guard is skipped, a guard that fails allows
the action and says why, and the retired turn-start and turn-end events
are ignored quietly.
An inline `git -c push.*`, `remote.*` or `branch.*` setting only decides
what a push with no refspec sends: a refspec in the command wins over
them, as in git. The push check treated any such setting like xargs and
read the push as every branch, so `git -c push.default=matching push
origin main` was stopped as "pushes every branch at once".
`git switch -C copy && git push origin copy` resets `copy` to what's
checked out before it pushes, but the push check looked `copy` up as
it was before the command ran. When `copy` held main's code, the push
passed as needing no verify, though it sent the unverified HEAD. The
same went for `git checkout -B` and `git branch -f`.

The check now reads a branch the command resets as what it points it
at: what's checked out, or the start point `git branch -f` names.
The working-tree snapshot left out any file it failed to read, as if it
had been deleted, so a permission or I/O error could change the
fingerprint without a word and turn into a misleading "commit what
belongs in it" denial. It now leaves out only files that are gone
(ENOENT, ENOTDIR) and throws on anything else, which the push check
reports as a guard failure that allowed the action.
JSON writes a line continuation in the command as `\\\n`, so the text
between `pr` and `create` holds backslashes and an `n`. The hook's
filter still passes it on, since the `*` after its bracket expression
is a glob that takes any text, not a repeat of the bracket. The test
now runs `gh pr create`, `gh pr new` and `git push` split across lines
through both hook commands.
The three hook runs for commands split across lines made the first
wiring test take over vitest's five-second limit while the rest of
`npm run verify` ran alongside it.
…mit takes

For `git commit … && git push`, the push check compared the working
tree with the verify record, as if the commit took all of it. A commit
can take less: `git commit -am` leaves out untracked files, a plain
commit leaves out unstaged changes, and named paths or `--patch` leave
out the rest. Such a push went through though the commit held less
than verify checked.

The check now reads what the command stages and what the commit takes.
It uses the working tree only when the commit holds all of it:
everything staged first (`git add -A`, `git add .` from the top
folder), `git commit -a` with no untracked files, or a plain commit
with nothing unstaged. Otherwise it stops the push and says to stage
everything in the same command, or to commit first and push on its own.
@scottlovegrove
scottlovegrove force-pushed the scottl/agent-push-check branch from 6c2daca to 916107c Compare November 27, 2026 14:30
@scottlovegrove
scottlovegrove merged commit 217886c into main Nov 27, 2026
6 checks passed
@scottlovegrove
scottlovegrove deleted the scottl/agent-push-check branch November 27, 2026 14:35
scottlovegrove added a commit to todoist/todoist-mcp that referenced this pull request Nov 27, 2026
# Pull Request

## Short description

Part 1 of 3. Replaces the end-of-turn gate with the push check from
todoist/todoist-cli#672. Before each shell command, the Claude Code and
Codex hooks (now one `PreToolUse` hook on `Bash`, replacing
`UserPromptSubmit` and `Stop`) call `.agents/scripts/guard.ts`, which
stops an agent's `git push` or `gh pr create` until `npm run verify` has
passed on exactly the commit being pushed. It reads the shell command
without running it, fingerprints the pushed commit from `git ls-tree`
(so the working-tree snapshot now hashes files the way git hashes a
blob), and lets through what verify records nothing for. Pushes a person
runs in their own terminal never go through it. `DISABLE_AGENT_GUARD=1`
still turns it off, and a guard that fails to run allows the action and
says why. The guard ignores the retired `turn-start`/`turn-end` events
that older configs still send.

Where todoist-mcp's tooling differs from todoist-cli's, this keeps
todoist-mcp's version: its verify steps, `ci.yml` as the one workflow a
local test reads, and the vite build. Only the tree.ts and git.ts pieces
the push check needs came across (blob-id hashing, deleted files left
out of the snapshot, docs counted by name, `snapshotCommit`,
`commitChanges`, `changeNeedsChecks`, `resolveCommit`, `findRepoRoot`,
`branchBase` with a head). Verify now treats a deleted doc as code too,
so it agrees with the push check.

What counts as docs changes here. The format check (oxfmt) reads
Markdown, JSON and YAML, so a README or workflow edit fails CI's format
check without any code changing. Only `CHANGELOG.md` and `LICENSE` are
docs now, and `.nvmrc` (read only by the release workflow) is the one
CI-only file. A test in `tree.test.ts` makes every file classified that
way badly formatted and checks oxfmt still passes.

It carries the fixes Doistbot found when the same port went into
comms-cli, outline-cli and zendesk-cli, each with a test that fails
without it:

- A commit and push in one command uses the working tree only when the
commit takes all of it (comms-cli, todoist/comms-cli#135): everything
staged first, `commit -a` with no untracked files, or a plain commit
with nothing unstaged. `git commit -am x && git push` with an untracked
file is stopped.
- That working tree stands in only for what the commit moves
(outline-cli, todoist/outline-cli#193), so `git commit -qam x && git
push origin v1` checks the tag's commit. I went a step further than
outline-cli: the check follows which branch is checked out when the
commit runs, so `git branch c && git commit … && git push origin c` and
a push of the old branch after `git switch -c` are checked against what
they hold, and `@` counts as HEAD on a detached HEAD.
- `git switch -C` and `git checkout -B` are checked against the commit
they force the branch to, and `git branch -f`/`-m`/`-c` and `git
update-ref` stop a push in the same command like a branch switch
(zendesk-cli, todoist/zendesk-cli#282). `git checkout <commit> -- file`
is a file restore, not a move; zendesk-cli's version also let `git
checkout <branch> --` through, which does switch, so that form still
counts as a move here.
- `popd` follows `pushd`'s stack (zendesk-cli).
- `xargs` long options that take a value (`--max-args`, `--max-procs`,
`--replace` and the rest) and `sudo --chdir`/`-D` are parsed
(outline-cli).
- `gh pr create -H<branch>` is read (outline-cli).
- `git push --tags` checks every tag `main` doesn't already contain
(outline-cli 8e24cd1), so release tags push freely and a tag on
unverified code doesn't. A push that follows tags (`--follow-tags` or
`push.followTags`) checks the annotated tags the pushed commits reach
that `main` doesn't contain.
- A branch the command makes or commits to counts whether the push names
it as `publish`, `heads/publish` or `refs/heads/publish`.
- An inline `git -c push.*`/`remote.*` setting no longer overrides a
refspec in the command (comms-cli, zendesk-cli).
- The snapshot fails on a file it can't read (`EACCES`) instead of
leaving it out of the fingerprint (comms-cli, outline-cli).

I tried it with real agents, each in a throwaway clone pushing to a
local bare repository. Claude Code (`claude -p`, Haiku) was denied with
the guard's reason on an unverified commit, ran `npm run verify`, and
the same push went through; the transcript shows the `PreToolUse` denial
and the bare repo has the branch. Codex 0.172 did the same in a trusted
plain clone (in a linked worktree it runs the main checkout's
`.codex/hooks.json`, as docs/agents.md says). Codex users need to run
`codex --trust` in their main checkout and review the new hook in
`/hooks` once.

## PR Checklist

- [x] Added tests for bugs / new features
- [x] Updated docs (README, etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚢 Ship PR requires passing checks before merge, but no independent human review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants